﻿using System;
using System.Drawing;

using We.Moonlight;

namespace We.Moonlight.IndependentDrawing
{
    public delegate void BackgroundDrawingHandler(ref Graphics G, ref Bitmap Background, Size Sz);

    public static class IndependentDrawing
    {
        public static void DrawBackgroundImage_Center(ref Graphics G, ref Bitmap Background, Size Sz)
        {
                G.DrawImageUnscaled(
                    Background,
                    (int)((Sz.Width - Background.Width) / 2),
                    (int)((Sz.Height - Background.Height) / 2)
                    );
        }
        public static void DrawBackgroundImage_Stretch(ref Graphics G, ref Bitmap Background, Size Sz)
        {
                G.DrawImage(Background, new Point[] { new Point(0, 0), new Point(Sz.Width, 0), new Point(0, Sz.Height) });
        }
        public static void DrawBackgroundImage_Tile(ref Graphics G, ref Bitmap Background, Size Sz)
        {
                for (int xcoord = 0; xcoord < Sz.Width; xcoord += Background.Size.Width + 1)
                {
                    for (int ycoord = 0; ycoord < Sz.Height; ycoord += Background.Size.Height + 1)
                    {
                        G.DrawImageUnscaled(Background, new Point(xcoord, ycoord));
                    }
                }
        }
        public static void DrawBackgroundImage_FitH(ref Graphics G, ref Bitmap Background, Size Sz)
        {
                G.DrawImage(
                    Background, 
                    0, 
                    -(int)((((Sz.Width * Background.Height) / Background.Width) - Sz.Height)/2), 
                    Sz.Width, (int)((Sz.Width * Background.Height) / Background.Width)
                    );
        }
        public static void DrawBackgroundImage_FitV(ref Graphics G, ref Bitmap Background, Size Sz)
        {
                G.DrawImage(
                    Background,
                    -(int)((((Sz.Height * Background.Width) / Background.Height) - Sz.Width) / 2),
                    0,
                    (int)((Sz.Height * Background.Width) / Background.Height), 
                    Sz.Height
                    );
        }

        public static BackgroundDrawingHandler ToBackgroundDrawingHandler(this Layout L)
        {
            switch (L)
            {
                case Layout.Tile: return new BackgroundDrawingHandler(DrawBackgroundImage_Tile);
                case Layout.Stretch: return new BackgroundDrawingHandler(DrawBackgroundImage_Stretch);
                case Layout.FitHorizontally: return new BackgroundDrawingHandler(DrawBackgroundImage_FitH);
                case Layout.FitVertically: return new BackgroundDrawingHandler(DrawBackgroundImage_FitV);
                default: return new BackgroundDrawingHandler(DrawBackgroundImage_Center);
            }
        }
        public static Layout ToLayout (this BackgroundDrawingHandler BDH)
        {
            if (BDH == null) throw new ArgumentNullException ("Cannot \"Cast\" a null BackgroundDrawingHandler to a Layout.");
            switch (BDH.Method.Name)
            {
                case "DrawBackgroundImage_Center":
                    return Layout.Center;
                case "DrawBackgroundImage_Stretch":
                    return Layout.Stretch;
                case "DrawBackgroundImage_Tile":
                    return Layout.Tile;
                case "DrawBackgroundImage_FitH":
                    return Layout.FitHorizontally;
                default:
                    return Layout.FitVertically;
            }
        }
    }
}